package com.tiantianyuegou.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tiantianyuegou.activity.BaseActivity;
import com.tiantianyuegou.R;
import com.tiantianyuegou.adapter.MyCouponAdapter;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.http.SPHelper;
import com.tiantianyuegou.model.CouponModel;
import com.tiantianyuegou.util.LoadingDialog;
import com.tiantianyuegou.util.NetworkUtils;
import com.tiantianyuegou.util.TimeUtils;
import com.tiantianyuegou.util.Tools;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 优惠卷
 */
public class MyCouponActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private ImageView img_title, img_no_news;
    private TextView text_title, text_menu, text_no_news;
    private ListView list_my_news;

    private String TAG="MyCouponActivity";

    private MyCouponAdapter couponAdapter;
    private List<CouponModel>couponModelList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_coupon);
        init();
        Tools.changeSystemBar(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadCoupon();
    }

    @Override
    public void findViewByIds() {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    private void init() {

        text_title = (TextView) findViewById(R.id.text_title);
        img_title = (ImageView) findViewById(R.id.img_title);

        text_title.setText(R.string.coupon_title);
        img_title.setOnClickListener(this);

        img_no_news = (ImageView) findViewById(R.id.img_no_news);
        text_no_news = (TextView) findViewById(R.id.text_no_news);
        list_my_news = (ListView) findViewById(R.id.list_my_news);

        img_no_news.setVisibility(View.VISIBLE);
        text_no_news.setVisibility(View.VISIBLE);
        list_my_news.setVisibility(View.GONE);

    }
    LoadingDialog loadingDialog;
    private void loadCoupon(){
        loadingDialog = new LoadingDialog(MyCouponActivity.this);
        if(!NetworkUtils.isNetworkAvailable(MyCouponActivity.this)){
            showMsg("请检查网络");
            return;
        }
        loadingDialog.show();
        couponModelList = new ArrayList<>();
        RequestParams params=new RequestParams();
        params.addBodyParameter("tmId", SPHelper.getTmId());
        params.addBodyParameter("status","2");
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.TICKET_LIST, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                loadingDialog.dismiss();
                String response=responseInfo.result;
                Log.e(TAG,"Coupon     "+response);
                try {
                    JSONObject root=new JSONObject(response);
                    String code = root.getString("code");
                    String list = root.getString("list");
                    if (code.equals("100")){
                      if(list.contains("[")){
                          JSONArray listArray = new JSONArray(list);
                          for (int i=0;i<listArray.length();i++){
                              JSONObject temp = listArray.getJSONObject(i);
                              String tsId =  temp.getString("tsId");
                              String tsName = temp.getString("tsName");
                              int ttId = temp.getInt("ttId");
                              String ttNumber = temp.getString("ttNumber");
                              String ttMoney = temp.getString("ttMoney");
                              long tStart = temp.getLong("ttStart");
                              long tEnd = temp.getLong("ttEnd");
                              String ttStart =   TimeUtils.convertTimeYMD1(tStart);
                              String ttEnd  = TimeUtils.convertTimeYMD1(tEnd);
                              String ttTop = temp.getString("ttTop");
                              CouponModel model = new CouponModel(ttId,ttMoney,ttTop,tsName,ttNumber,ttStart,ttEnd);
                              couponModelList.add(model);
                          }
                          list_my_news.setVisibility(View.VISIBLE);
                          img_no_news.setVisibility(View.GONE);
                          text_no_news.setVisibility(View.GONE);
                          couponAdapter = new MyCouponAdapter(MyCouponActivity.this, couponModelList);
                          list_my_news.setAdapter(couponAdapter);
                          couponAdapter.notifyDataSetChanged();
                          if(couponModelList.size() == 0){
                              img_no_news.setVisibility(View.VISIBLE);
                              text_no_news.setVisibility(View.VISIBLE);
                              list_my_news.setVisibility(View.GONE);
                          }
                      }
                    }else{
                        img_no_news.setVisibility(View.VISIBLE);
                        text_no_news.setVisibility(View.VISIBLE);
                        list_my_news.setVisibility(View.GONE);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                loadingDialog.dismiss();
                showMsg("请求服务器失败");
                img_no_news.setVisibility(View.VISIBLE);
                text_no_news.setVisibility(View.VISIBLE);
                list_my_news.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_title:
                finish();
                break;

            default:
                break;
        }
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }
}
